
cc.Class({
    extends: cc.Component,
    properties: {
        hambhamburgerChild:[cc.Prefab],
        hamburgerGuest:[cc.Prefab],
        coffee:[cc.Prefab],
        chengzhi:[cc.Prefab],
        kele:[cc.Prefab],
        waitTime: 25,
        progress:cc.Sprite,
        showNode:cc.Node,
        personSprite:[cc.SpriteFrame],
        personAngrySprite:[cc.SpriteFrame],
        personBigAngrySprite:[cc.SpriteFrame],
        cryNode:cc.Node,
    },
    // LIFE-CYCLE CALLBACKS:
    onLoad () {

    },
    start () {
        var ran = Math.floor(Math.random()*5);
        this.ranType = ran;
        this.node.getComponent(cc.Sprite).spriteFrame = this.personSprite[ran];
        this.startTime = false;
        this.angry = false;
        this.mixTime = 0; //计算材料生成的最短时间
        this.bigAngry = false;
        this.game = cc.find("Canvas").getComponent('Home');
        this.demand();
        console.log( this.mixTime)
    },
    leave(bol){
        if(!this.startTime) return;
        this.startTime = false;
        this.node.group = 'default';
        this.node.children[0].active = false;
        this.game.guestPosition.push(cc.p(this.node.x,this.node.y));
        if(this.node.x > 0){
            var time = 2;
        }else{
            var time = 3;
        }
        this.node.runAction(cc.repeatForever(cc.sequence(cc.moveBy(0.15,0,10),cc.moveBy(0.15,0,-10))));
        this.node.runAction(cc.sequence(cc.moveTo(time,cc.p(1200,-20)),cc.callFunc(function(){

        },this)));
        if(bol){
            if(25-this.waitTime < this.mixTime + 3){
                cc.voicePlay.playEffect('orderok');
                cc.coinWin +=3
            }else{
                cc.voicePlay.playEffect('orderok2');
            }
            cc.diamondWin = cc.diamondWin + cc.doubleDiamond;
            cc.configData.guestNum ++;
            var time = Math.round(1 + Math.random()*3);
        }else{
            var time = Math.round(10 + Math.random()*2);
        }
        this.scheduleOnce(function(){
            this.game.guestComing();
            this.node.destroy();
        },time)
    },
    hamburgerGet(){
         var ran = [0,1,2,3,4];
         var len = 0 + Math.floor(Math.random()*5);
         ran.sort(function(){
            return Math.random() - 0.5
         });
         var arr = ran.slice(0,len);
         arr.unshift(0);
         return arr.join('');
         },
    demand(){
        var ran = Math.floor(Math.random()*3);
       if(ran == 0){
           this.demandList = [
               {cup: '' + Math.floor(Math.random()*3)}, //三种类型的水
               {hamburger:this.hamburgerGet()}
       ]
       }else if(ran == 1){
           this.demandList = [{
               cup: '' + Math.floor(Math.random()*3)
           }//三种类型的水
       ]
       }else if(ran == 2){
           this.demandList = [{
               hamburger:this.hamburgerGet()
           }
       ]
       }
        this.showDemand();
    },
    showDemand(){
     this.showNode.removeAllChildren();
     for(var i =0;i<this.demandList.length;i++){
        for(var prop in this.demandList[i]){
            if(prop == 'cup'){
                if(this.demandList[i][prop] == 0){
                    this.mixTime += cc.configData.material.coffee.time[cc.configData.material.coffee.level];
                    this.showNode.addChild(cc.instantiate(this.coffee[cc.configData.material.coffee.level]))//对应等级的饮料
                }else if(this.demandList[i][prop] == 1){
                    this.mixTime += cc.configData.material.chengzhi.time[cc.configData.material.chengzhi.level];
                    this.showNode.addChild(cc.instantiate(this.chengzhi[cc.configData.material.chengzhi.level]))//
                }else if(this.demandList[i][prop] == 2){
                    this.mixTime += cc.configData.material.kele.time[cc.configData.material.kele.level];
                    this.showNode.addChild(cc.instantiate(this.kele[cc.configData.material.kele.level]))//
                }
            }
            if(prop == 'hamburger'){
                var hamburgerGuest = cc.instantiate(this.hamburgerGuest[cc.configData.material.hamburger.level]);
                this.mixTime += cc.configData.material.meat.time[cc.configData.material.meat.level];
                hamburgerGuest.children[1].zIndex = 99;
                this.showNode.addChild(hamburgerGuest);
                var arr = this.demandList[i][prop].split('');
                for(var j=0;j<arr.length;j++){
                        hamburgerGuest.addChild(cc.instantiate(this.hambhamburgerChild[arr[j]]))//this.demandList[i][prop][0]
                }
                hamburgerGuest.sortAllChildren();
            }
        }
     }
    },
    accounts(type,value){
        console.log('开始结算');
        this.check =true;
        var deleteDemand = 0;
        if(this.demandList.length == 2){
             if(this.demandList[1][type] !== undefined){
                 if(value === this.demandList[1][type]){
                     this.check = true;
                     deleteDemand = 1;
                 }else{
                     this.check = false;
                 }
             }else if(this.demandList[0][type] !== undefined) {
                 if(value === this.demandList[0][type]){
                     this.check = true;
                     deleteDemand = 0;
                 }else{
                     this.check = false;
                 }
             }else{
                 this.check =false;
             }
        }else if(this.demandList.length == 1){
            if(value === this.demandList[0][type]){
                this.check = true;
                deleteDemand = 0;
            }else{
                this.check = false;
            }
        }
        if(this.check){
            this.demandList.splice(deleteDemand,1);
            if(this.demandList.length == 0){
                console.log('付款滚蛋');
                this.leave(true);
            }else{
                this.showDemand();
            }
            return true;
        }else{
            console.log('这不是我要点的东西')
            return false;
        }
    },
    changeAngry(){
        this.node.getComponent(cc.Sprite).spriteFrame = this.personAngrySprite[this.ranType];
    },
    changeBigAngry(){
        if(this.ranType === 1 ||this.ranType === 3){
       this.node.getComponent(cc.Sprite).spriteFrame = this.personBigAngrySprite[this.ranType];
       this.cryNode.active = true;
        }else{
            this.node.getComponent(cc.Animation).play('people'+this.ranType);
      }

    },
     update (dt) {
         if(!this.startTime) return;
         this.waitTime -= dt;
         this.progress.fillRange = this.waitTime / 25;
         if(this.waitTime <= 12 && !this.angry){
             this.angry = true;
             this.changeAngry();
         }
         if(this.waitTime <= 2 && !this.bigAngry){
             this. bigAngry = true;
             this.changeBigAngry();
         }
         if(this.waitTime <= 0){
             this.leave();
         }
     },
});
